package com.hiltony.web.controller;

import com.hiltony.web.model.*;
import com.hiltony.web.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by YX on 2016/9/16.
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, UserInfo user){

        try {
            user.setState(1);
            UserInfo userInfo = userService.getUser(user);
            if (userInfo!=null && userInfo.getId()!=null)
                return "success";
            else
                return "fail";
        }
        catch (Exception e){
            e.printStackTrace();
            return "error";
        }


    }
    @RequestMapping("/showUser")
    public String toIndex(HttpServletRequest request, Model model){
        int userId = Integer.parseInt(request.getParameter("id"));
        UserInfo user = this.userService.getUserById(userId);
        model.addAttribute("user", user);
        return "showUser";

    }

    @RequestMapping("/getUserInfo")
    @ResponseBody
    public UserInfo getUserInfo(){
        UserInfo userInfo  = userService.getUserById(1);

        return userInfo;
    }
    @RequestMapping("/listUser")
    public String toIndex(UserInfo userInfo, HttpServletRequest request, Model model) {
        List<UserInfo> users = new ArrayList<UserInfo>();

        users.addAll(userService.getUsers(userInfo));
        model.addAttribute("userList", users);
        return "user";

    }

    @RequestMapping("/userDetail")
    public String getMemberDetail(UserInfo userInfo, HttpServletRequest request, Model model) {

        model.addAttribute("user", userService.getUser(userInfo));
        return "user_detail";

    }


    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public String addUser(@RequestBody UserInfo userInfo, HttpServletRequest request, Model model) {

        UserInfo query = new UserInfo();
        query.setUserName(userInfo.getUserName());
        if(userService.getUser(query)!=null){
            return "userExists";
        }
        try {
            userService.insert(userInfo);
        } catch (Exception e){
            RuntimeException e1 = (RuntimeException) e;
            if (e1.getMessage().startsWith("账户名不得重复")){
                return "账户名不得重复";
            }
        }

        return "success";
    }



    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    public String updateMember(@RequestBody UserInfo userInfo, HttpServletRequest request, Model model) throws ParseException {
        userService.updateUser(userInfo);
        return "success";
    }


}

